'use strict'

const Joi = require('joi')
const t = (module.exports = require('../tester').createServiceTester())
const {
  twoVulnerabilitiesSvg,
  zeroVulnerabilitiesSvg,
} = require('./snyk-test-helpers')

t.create('live: valid package latest version')
  .get('/mocha.json')
  .timeout(10000)
  .expectBadge({
    label: 'vulnerabilities',
    message: Joi.number().required(),
  })

t.create('live: valid scoped package latest version')
  .get('/@babel/core.json')
  .timeout(10000)
  .expectBadge({
    label: 'vulnerabilities',
    message: Joi.number().required(),
  })

t.create('live: non existent package')
  .get('/mochaabcdef.json')
  .timeout(10000)
  .expectBadge({
    label: 'vulnerabilities',
    message: 'npm package is invalid or does not exist',
  })

t.create('live: valid package specific version')
  .get('/mocha@4.0.0.json?style=_shields_test')
  .timeout(10000)
  .expectBadge({
    label: 'vulnerabilities',
    message: '1',
    color: 'red',
  })

t.create('live: non existent package version')
  .get('/gh-badges@0.3.4.json')
  .timeout(10000)
  .expectBadge({
    label: 'vulnerabilities',
    message: 'npm package is invalid or does not exist',
  })

t.create('package has no vulnerabilities')
  .get('/mocha.json?style=_shields_test')
  .intercept(nock =>
    nock('https://snyk.io/test/npm/mocha')
      .get('/badge.svg')
      .reply(200, zeroVulnerabilitiesSvg)
  )
  .expectBadge({
    label: 'vulnerabilities',
    message: '0',
    color: 'brightgreen',
  })

t.create('package has vulnerabilities')
  .get('/mocha.json?style=_shields_test')
  .intercept(nock =>
    nock('https://snyk.io/test/npm/mocha')
      .get('/badge.svg')
      .reply(200, twoVulnerabilitiesSvg)
  )
  .expectBadge({
    label: 'vulnerabilities',
    message: '2',
    color: 'red',
  })

t.create('package not found')
  .get('/not-mocha-fake-ish@13.0.0.json?style=_shields_test')
  .intercept(nock =>
    nock('https://snyk.io/test/npm/not-mocha-fake-ish@13.0.0')
      .get('/badge.svg')
      .reply(200, '<html>foo</html>')
  )
  .expectBadge({
    label: 'vulnerabilities',
    message: 'npm package is invalid or does not exist',
    color: 'red',
  })
